Page({
  data: {
    goodsList:[],   //商品列表
    chooseList:[],  //以选商品列表
    sortType:1,     //排序类别1顺序0逆序
    total:0         //总计价格
  },
  //bindTapChangeSortType 点击排序响应函数
  bindTapChangeSortType(){
    this.setData({sortType:!this.data.sortType});
    var sortType = this.data.sortType;
    var newdata = this.data.goodsList;
    function compair(front,back){
      //排序类别会决定前后比较的方式
      if(sortType) return front>back;
      return front<back;
    }
    //冒泡排序
    for(let i=0;i<newdata.length-1;i++){
      for(let j=i+1;j<newdata.length;j++){
        if(compair(newdata[j].price[newdata[j].priceIndex],newdata[i].price[newdata[i].priceIndex])){
          //如果比较符合,则会调换前后位置
          [ newdata[j],newdata[i] ] = [ newdata[i],newdata[j] ];
        }
      }
    }
    this.setData({goodsList:newdata});//数据更新
  },
  //bindTapChooseAll 点击选择全部商品响应函数
  bindTapChooseAll(){
    if(this.data.chooseList.length>0){
      this.setData({chooseList:[]});
    }else{
      var list = [];
      this.data.goodsList.forEach((item)=>{list.push(item.gid)});
      this.setData({chooseList:list});
    }
    this.getTotal();
  },
  //bindTapSingleChoose 点击选择单个商品响应函数
  bindTapSingleChoose(e){
    var newlist = this.data.chooseList;
    var gid = e.target.dataset.gid;
    if(newlist.includes(gid) == false){
      newlist.push(gid);
      this.setData({chooseList:newlist});
    }
    else{
      newlist.splice(newlist.indexOf(gid),1);
      this.setData({chooseList:newlist});
    }
    this.getTotal();
  },
  //bindTapDeleteChosen 点击删除选中响应函数
  bindTapDeleteChosen(){
    var that = this;
    wx.showModal({
      title: '确认',
      content: '是否删除',
      showCancel:true,
      complete: (res) => {
        if (res.cancel) {
          console.log('删除取消');
        }
        if (res.confirm) {
          that.deleteChosen();
        }
      }
    });
  },
  //deleteChosen 删除已选函数
  deleteChosen(){
    //数据库删除
    var app = getApp();
    var deleteList = this.data.chooseList.join(' ');
    app.pinia('deleteShopCart',{
      cid:app.globalData.clientInfo.cid,
      gidList:deleteList
    },(res)=>{
      app.globalData.shopcart = res.data;
    })
    //页面内删除
    var olddata = this.data.goodsList;
    var newdata = olddata.filter((outItem)=>{
      let flag = true;
      this.data.chooseList.forEach(inItem=>{
        if(outItem.gid == inItem) flag = false;
      });
      return flag;
    });
    this.setData({chooseList:[]});
    this.setData({goodsList:newdata});
    this.getTotal();
  },
  //bindTapBuyChosen 点击购买响应函数
  bindTapBuyChosen(){
    //预处理数据
    var olddata = this.data.goodsList;
    var newdata = olddata.filter((item)=>this.data.chooseList.includes(item.gid));
    newdata.forEach(item=>{
      delete item.gtype;
      delete item.name;
      delete item.imgUrl;
      item.total = item.price[item.priceIndex] * item.num;
      delete item.price;
      delete item.priceIndex;
    });
    var dataString = JSON.stringify(newdata);
    var that = this;
    (getApp()).pinia('addOrder',{
      orderList:dataString
    },(res)=>{
      console.log(res.data);
      wx.showToast({
        title: '购买成功',
        icon:'success'
      });
      that.deleteChosen();
    });//
  },
  //bindTapNavToDetail 点击跳转到商品详情页响应函数
  bindTapNavToDetail(e){
    (getApp()).pinia('getGoodDetail',{
      gid:e.target.dataset.gid
    },(res)=>{
      let obj = res.data
      wx.navigateTo({
        url: '/pages/goodDetail/goodDetail',
        success: function(res) {
          res.eventChannel.emit('goodData', { data: [obj] });
        }
      });      
    });
  },
  //getTotal 计算总价函数
  getTotal(){
    var goodsList = this.data.goodsList;
    var chooseList = this.data.chooseList;
    var total = 0;
    for(var i = 0;i<chooseList.length;i++){
      for(var j = 0;j<goodsList.length;j++){
        if(goodsList[j].gid==chooseList[i]){
          total+=goodsList[j].price[goodsList[j].priceIndex]*goodsList[j].num;
        }
      }
    }
    this.setData({total:total});//
  },
  onLoad(options) {
    (getApp()).isLogined();//检查是否登录
    this.setData({themeIndex:(getApp()).globalData.themeIndex});//设置主题
    this.setData({goodsList:(getApp()).globalData.shopcart});//获取购物车列表
  }
})